<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 如果已经登录，直接跳转到首页
if (isset($_SESSION['admin_id'])) {
    header("Location: index.php");
    exit();
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];
    $captcha = strtolower(trim($_POST['captcha']));
    
    // 验证输入
    if (empty($username) || empty($password)) {
        $error = '用户名和密码不能为空';
    } 
    // 验证验证码
    elseif (empty($captcha) || $captcha !== $_SESSION['captcha_code']) {
        $error = '验证码错误';
    } 
    else {
        $db = db_connect();
        
        // 查询用户
        $stmt = $db->prepare("SELECT * FROM admin_users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();
        
        if ($user && password_verify($password, $user['password'])) {
            if ($user['status'] == 0) {
                $error = '账号已被禁用';
            } else {
                // 更新最后登录时间
                $stmt = $db->prepare("UPDATE admin_users SET last_login = NOW() WHERE id = ?");
                $stmt->execute([$user['id']]);
                
                // 存储必要的session信息
                $_SESSION['admin_id'] = $user['id'];
                $_SESSION['name'] = $user['name'];
                $_SESSION['role'] = $user['role'];
                
                // 跳转到后台首页
                header('Location: index.php');
                exit;
            }
        } else {
            $error = '用户名或密码错误';
        }
    }
    // 无论成功与否，都重新生成验证码
    unset($_SESSION['captcha_code']);
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body class="login-page">
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2><?php echo SITE_NAME; ?></h2>
                <p>欢迎回来，请登录您的账号</p>
            </div>
            
            <?php if ($error): ?>
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo $error; ?>
            </div>
            <?php endif; ?>
            
            <form method="POST" class="login-form">
                <div class="form-group">
                    <label for="username">
                        <i class="fas fa-user"></i>
                        <span>用户名</span>
                    </label>
                    <input type="text" id="username" name="username" placeholder="请输入用户名" required>
                </div>
                
                <div class="form-group">
                    <label for="password">
                        <i class="fas fa-lock"></i>
                        <span>密码</span>
                    </label>
                    <input type="password" id="password" name="password" placeholder="请输入密码" required>
                </div>

                <div class="form-group captcha-group">
                    <label for="captcha">
                        <i class="fas fa-shield-alt"></i>
                        <span>验证码</span>
                    </label>
                    <div class="captcha-input">
                        <input type="text" id="captcha" name="captcha" placeholder="请输入验证码" required>
                        <img src="captcha.php" alt="验证码" id="captcha-img" onclick="this.src='captcha.php?t='+Math.random()">
                    </div>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn-login">
                        <i class="fas fa-sign-in-alt"></i>
                        登录系统
                    </button>
                </div>
            </form>
        </div>
    </div>

    <style>
    .login-page {
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #00CED1, #1E90FF);
        padding: 20px;
    }

    .login-container {
        width: 100%;
        max-width: 400px;
    }

    .login-box {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 10px;
        padding: 30px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
    }

    .login-header {
        text-align: center;
        margin-bottom: 30px;
    }

    .login-header h2 {
        color: #333;
        font-size: 24px;
        margin-bottom: 10px;
    }

    .login-header p {
        color: #666;
        font-size: 14px;
    }

    .alert {
        padding: 12px 15px;
        border-radius: 6px;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .alert-danger {
        background-color: #fff2f0;
        border: 1px solid #ffccc7;
        color: #ff4d4f;
    }

    .form-group {
        margin-bottom: 20px;
    }

    .form-group label {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 8px;
        color: #333;
        font-size: 14px;
    }

    .form-group input {
        width: 100%;
        padding: 10px 12px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-size: 14px;
        transition: all 0.3s;
        background: #f8f9fa;
    }

    .form-group input:focus {
        border-color: #1E90FF;
        box-shadow: 0 0 0 2px rgba(30, 144, 255, 0.2);
        background: #fff;
        outline: none;
    }

    .btn-login {
        width: 100%;
        padding: 12px;
        background: #1E90FF;
        color: #fff;
        border: none;
        border-radius: 6px;
        font-size: 15px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        transition: all 0.3s;
    }

    .btn-login:hover {
        background: #00CED1;
    }

    @media (max-width: 480px) {
        .login-box {
            padding: 20px;
        }
    }

    .captcha-group {
        margin-bottom: 20px;
    }

    .captcha-input {
        display: flex;
        gap: 10px;
        align-items: center;
    }

    .captcha-input input {
        flex: 1;
    }

    #captcha-img {
        height: 40px;
        border-radius: 4px;
        cursor: pointer;
    }

    .captcha-input input {
        width: calc(100% - 110px);
    }
    </style>

    <script>
    // 点击验证码图片刷新
    document.getElementById('captcha-img').onclick = function() {
        this.src = 'captcha.php?t=' + Math.random();
    };
    </script>
</body>
</html> 